package com.sec.controller;

import com.sec.dao.mysql.view.UserViewDao;
import com.sec.domain.mysql.view.UserView;
import com.sec.util.Pager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Resource
    private UserViewDao userViewDao;

    @RequestMapping(value="/", method = RequestMethod.GET)
    public String index_page () {
        return "/article/index.html";
    }

    @RequestMapping(value="/index", method = RequestMethod.GET)
    public String index () {
        Object user = null;
        if(SecurityContextHolder.getContext().getAuthentication() != null){
            user = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }
        if (user!=null && user instanceof UserDetails) {
            return "/article/index.html";
        } else {
            return "/page-login.html";
        }
    }


    /**
     * 用户退出
     * */
    @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "/index";
    }

    /**
     * 用户登录
     * */
    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login () {
        return "/page-login.html";
    }


    /**
     * 用户权限查看
     * */
    @RequestMapping(value="/userView", method = RequestMethod.GET)
    public String userView (Model model) {
        List<UserView> userViewList = userViewDao.queryAll();
        model.addAttribute("userViewList", userViewList);
        return "/userView.jsp";
    }

    /**
     * 测试
     * */
    @RequestMapping(value="/test", method = RequestMethod.GET)
    @ResponseBody
    public String tset () {
        Pager pager = new Pager();
        pager.setHql("from UserView where userId = :userId");
        pager.setPage(1);
        pager.setRows(1);
        Map<String, Object> parm = new HashMap<>();
        parm.put("userId", 2l);
        Pager result = userViewDao.pagerff(pager, parm);
        System.out.println(result.getList().size());
        return result.toString();
    }
}
